/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.model.entity.provider;

// Generated Dec 6, 2009 5:47:57 PM by Hibernate Tools 3.2.2.GA
// powered by MAGS

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.mags.dao.provider.CacheableDataProvider;
import org.mags.remas.model.entity.EntityType;

public class EntityTypeProvider extends CacheableDataProvider<EntityType> {
	private static final long serialVersionUID = 1L;
	private EntityCategoryProvider entityCategoryProvider;
	
	public EntityCategoryProvider getEntityCategoryProvider() {
		return entityCategoryProvider;
	}
	public void setEntityCategoryProvider(
			EntityCategoryProvider entityCategoryProvider) {
		this.entityCategoryProvider = entityCategoryProvider;
	}
	public EntityTypeProvider(EntityCategoryProvider entityCategoryProvider) {
		super.setDefaultOrderBy(new String[][]{{"name", "asc"}});
		this.entityCategoryProvider = entityCategoryProvider;
	}
	public EntityType getROOT(short entityCategoryId) {
		return this.getByCode("ROOT", entityCategoryId);
	}
	public EntityType getByName(String name, int entityCategoryId) {
		for(EntityType i: this.getAll())
			if(name.equalsIgnoreCase(i.getName()))
				return i;
		return null;
	}
	public List<Integer> getIdsByCategoryId(short entityCategoryId) {
		List<Integer> entityTypeIds = new ArrayList<Integer>();
		for(EntityType i: this.getAll())
			if(i.getEntityCategory().getEntityCategoryId()==entityCategoryId)
				entityTypeIds.add(i.getEntityTypeId());
		return entityTypeIds;
	}
	public List<EntityType> getByCategoryId(short entityCategoryId) {
		List<EntityType> entityTypes = new ArrayList<EntityType>();
		for(EntityType i: this.getAll())
			if(i.getEntityCategory().getEntityCategoryId()==entityCategoryId)
				entityTypes.add(i);
		return entityTypes;
	}
	public EntityType getByCode(String code, int entityCategoryId) {
		for(EntityType i: this.getAll())
			if(code.equalsIgnoreCase(i.getCode()))
				return i;
		return null;
	}
	public EntityType getById(Serializable id) {
		for(EntityType i: this.getAll())
			if(i.getEntityTypeId()==((Integer)id).intValue())
				return i;
		return null;
	}
	protected void loadProperties(EntityType one) {
		one.setEntityCategory(entityCategoryProvider.getById(one.getEntityCategory().getEntityCategoryId()));
	}
}
